home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0887.arc / WEST1.TXT < prev    next >
Encoding:
Text File  |  1980-01-01  |  5.9 KB  |  291 lines

  1.  
  2.  
  3. ====================
  4.  
  5.      FILEIO.C
  6.  
  7. ====================
  8.  
  9.  
  10. /* Listing 8: Fileio benchmark */
  11.  
  12. #define LSC
  13.  
  14. /* fileio.c */
  15.  
  16.  
  17. /* file reading and writing benchmark
  18.    sequentially writes a 65000 byte file on disk
  19.    generates random long numbers
  20.    uses these modulo 65000 to read and write strings of ODDNUM bytes
  21.    with the file handling system of the c package
  22.    the random number generator is set to a specific seed,
  23.    so that all compilers should generate the same code
  24.  
  25.    Fixed by Joel West, April 16, 1987 to use UNIX-standard creat()
  26.    and open() parameters.
  27. */
  28.  
  29. #include <stdio.h>
  30.  
  31. #ifdef LSC
  32. #include <unix.h>
  33. #else
  34. #ifndef MACC            /* this is the right way to do it */
  35. #include <fcntl.h>
  36. #endif
  37. #endif
  38.  
  39. #ifndef MACC
  40. #define FILEMODE 0666   /* the normal rw-rw-rw */
  41. #else
  42. #define FILEMODE 0x7    /* Mac C only */
  43. #define O_RDONLY 0
  44. #define O_WRONLY 1
  45. #define O_RDWR   2
  46. #endif
  47.  
  48. #ifdef LSC
  49. #define abort AbOrT     /* Lightspeed C has an 'abort' entry point */
  50. #endif
  51.  
  52. #define ERROR -1
  53. #define READERR 0
  54. #define OKCLOSE 0
  55.  
  56. /* For lseek() */
  57. #define BEG 0
  58. #define CURR 1
  59. #define END 2
  60.  
  61. #define FILESIZE 65000L
  62. #define COUNT 500
  63.  
  64. #define C 13849L
  65. #define A 25173L
  66. #define ODDNUM 23
  67.  
  68. long seed = 7L;
  69. long random (), lseek ();
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. main ()
  78. {       int i;
  79.         long j, pos;
  80.         int fd;
  81.         char buffer [ODDNUM + 1];
  82.  
  83. #include "startup.c"
  84.  
  85.         if ((fd = creat ("test.dat", FILEMODE)) == ERROR)
  86.                 abort ("Can't create data file\n");
  87.         else
  88.                 printf("File opened for sequential writing\n");
  89.  
  90.         for (j = 0; j < FILESIZE; ++j)
  91.                 if (write(fd, "x", 1) == ERROR)
  92.                         abort ("Unexpected EOF in writing data file\n");
  93.  
  94.         if (close (fd) != OKCLOSE)
  95.                 abort ("Error closing data file\n");
  96.         else
  97.                 printf ("Normal termination writing data file\n");
  98.  
  99.         if ((fd = open ("test.dat", O_RDWR)) == ERROR)
  100.                 abort ("Can't open data file for random reading and
  101. writing\n");
  102.         else
  103.                 printf ("File opened for random reading and writing\n");
  104.  
  105.         for (i = 0; i < COUNT; ++i)
  106.         {       j = random (FILESIZE);
  107.                 if (j < 0L)
  108.                         j = (-j);
  109.                 if (FILESIZE - j < ODDNUM)
  110.                         continue;
  111.                 if ((pos = lseek (fd, j, BEG)) == -1L)
  112.                         abort ("Error reading at random offset\n");
  113.                 if (read (fd, buffer, ODDNUM) == READERR)
  114.                         abort ("Error reading at random offset\n");
  115.                 j = random (FILESIZE);
  116.                 if (j < 0L)
  117.                         j = (-j);
  118.                 if (FILESIZE - j < ODDNUM)
  119.                         continue;
  120.                 if ((pos = lseek (fd, j, BEG)) == -1L)
  121.                         abort ("Error seeking to random offset\n");
  122.                 if (write (fd, buffer, ODDNUM) == READERR)
  123.                         abort ("Error writing at random offset\n");
  124.         }
  125.         if (close (fd) != OKCLOSE)
  126.                 abort ("Error closing data file\n");
  127.         else
  128.                 printf ("Normal termination from random reading and
  129. writing\n");
  130.  
  131. #include "done.c"
  132. }
  133.  
  134.  
  135. long random (size)
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. long size;
  143. {       seed = seed * A + C;
  144.         return (seed % size);
  145. }
  146.  
  147. abort (message)
  148. char *message;
  149. {       printf (message);
  150.         exit (ERROR);
  151. }
  152.  
  153.  
  154. ====================
  155.  
  156.      DONE.C
  157.  
  158. ====================
  159.  
  160.  
  161. /* Listing 10: standard termination header */
  162.  
  163.  
  164. /* done.c */
  165.  
  166.  
  167. /* End timing */
  168.         time = TickCount() - time;
  169.         printf("ticks=%ld\n",time);
  170.         printf("Press any key to return to FINDER: ");
  171.         getchar();
  172.  
  173.  
  174. ====================
  175.  
  176.      FIB.C
  177.  
  178. ====================
  179.  
  180.  
  181. /* Listing 5: Fib benchmark */
  182.  
  183. #define LSC
  184.  
  185. /* Fib.c */
  186.  
  187. /* Fibonacci benchmark, modified by Joel West, 4/13/87 */
  188.  
  189. #include <stdio.h>
  190.  
  191. #ifdef HYPERC
  192. #include <TBXTypes.h>
  193. #include <events.h>
  194. #endif
  195.  
  196. #define REGISTER
  197.  
  198. #define NTIMES 10 /* number of times to compute Fibonacci value */
  199. #define NUMBER 24 /* biggest one we can compute with 16 bits */
  200.  
  201.  
  202. main()                  /* compute Fibonacci value */
  203. {       REGISTER short i;
  204.         REGISTER unsigned short value;
  205.         unsigned short fib();
  206.  
  207. #include "startup.c"
  208.  
  209.         for (i = 1; i <= NTIMES; i++)
  210.                 value = fib(NUMBER);
  211.  
  212. #include "done.c"
  213.  
  214.      exit(0);
  215. }
  216.  
  217.  
  218. unsigned short fib(x)           /* compute Fibonacci number recursively */
  219.  REGISTER short x;
  220. {       if (x > 2)
  221.                 return (fib(x - 1) + fib(x - 2));
  222.         else
  223.                 return (1);
  224. }
  225.  
  226.  
  227. ====================
  228.  
  229.      FLOAT.C
  230.  
  231. ====================
  232.  
  233.  
  234. /* Listing 7: Float benchmark */
  235.  
  236. #define LSC
  237.  
  238. /* float.c */
  239.  
  240. /* simple benchmark for testing floating point speed of c libraries
  241.    does repeated multiplications and divisions in a loop that is
  242.    large enough to make the looping time insignificant */
  243.  
  244. #include <stdio.h>
  245.  
  246. #ifdef MACC
  247. #define EXTENDED extended
  248. #else
  249. #ifdef HYPERC
  250. #define EXTENDED extended
  251. #include <TBXTypes.h>
  252. #include <events.h>
  253. #else
  254. #define EXTENDED double
  255. #endif
  256. #endif
  257.  
  258.  
  259. #define CONST1  3.141597E0
  260. #define CONST2 1.7839032E4
  261. #define COUNT 10000
  262.  
  263. main()
  264. {       EXTENDED a, b, c;
  265.         int i;
  266.  
  267. #include "startup.c"
  268.  
  269.         a = CONST1;
  270.         b = CONST2;
  271.         for (i = 0; i < COUNT; ++i)
  272.         {       c = a * b;
  273.                 c = c / a;
  274.                 c = a * b;
  275.                 c = c / a;
  276.                 c = a * b;
  277.                 c = c / a;
  278.                 c = a * b;
  279.                 c = c / a;
  280.                 c = a * b;
  281.                 c = c / a;
  282.                 c = a * b;
  283.                 c = c / a;
  284.                 c = a * b;
  285.                 c = c / a;
  286.         }
  287.  
  288.  
  289. #include "done.c"
  290. }
  291.